<?php

namespace Home\Controller;
use Common\Controller\Base;
use Common\Model\Model;
use Think\Controller;

class DoController extends Base {

    /**
     * 添加关注
     */
    public function addfans(){
        $this->check();
        $get=I('get.');
        $get['myid']=$this->userid;
        if($get['userid']==$this->userid){
            $this->error('自己不能关注自己!');
        }
        if(D('Home/Commons')->addfans($get)){
            $this->success('关注成功','do/cancelfans/');
        }else{
            $error = D('Home/Commons')->getError();
            $this->error($error ? $error : '关注失败,请稍候再试！');
        }
    }

    /**
     * 取消关注
     */
    public function cancelfans(){
        $this->check();
        $get=I('get.');
        $get['myid']=$this->userid;
        $fansModel=M('UserFans');
        $where['myid']=$this->userid;
        $where['userid']=$get['userid'];
        $data['status']=false;

        if($fansModel->where(array('myid'=>$get['myid'],'userid'=>$get['userid'],'status'=>false))->count()>0){
            $this->success('取消关注成功',"do/addfans/");
            return false;
        }
        $result=$fansModel->where($where)->save($data);
        if($result){
            M('User')->where(array('userid'=>$get['myid']))->setDec('attentionnum',1);
            M('User')->where(array('userid'=>$get['userid']))->setDec('fansnum',1);
            $this->success('取消关注成功',"do/addfans/");
        }else{
            $this->error('取消操作失败');
        }
    }

    /**
     * 留言
     */
    public function sendmsg(){
        $post=I('post.');
      
        $post['userid']=$this->userid;
        $this->check();
        $type=$post['type'];
        if($type==1){
            $table='Works';
            $map['workid']=$post['infoid'];         
        }elseif($type==2){
            $table='User';
            $map['userid']=$post['infoid'];
            if($post['infoid']==$post['userid']){
                $this->error('您不能给自己留言!');
            }
        }elseif($type==3){
            $table='TryReport';
            $map['id']=$post['infoid'];
        }elseif($type==4){
            $table='Suipai';
            $map['id']=$post['infoid'];
        }elseif($type==5){
            $table='News';
            $map['id']=$post['infoid'];
        }elseif($type==6){
            $table='Activity';
            $map['id']=$post['infoid'];
        }elseif($type==7)
        {
            $table="Teachlist";
            $map['teachid']=$post['infoid'];
        }

        //当天第一次留言
        $stime=strtotime(date('Y-m-d 00:00:00'));
        $etime=strtotime(date('Y-m-d 23:59:59'));
        $where['userid']= array('EQ',$this->userid);
        $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');

        $todayMsgnum=M('UserMessage')->where($where)->count();
		$post['parent_id'] = isset($post['parent_id']) ? intval($post['parent_id']) : 0;
        if(D('Home/Commons')->sendMessage($post,$type)){

            if($todayMsgnum<1){
                $userarr['upoint'] = array('exp','upoint+2');
                M('User')->where(array('userid'=>$this->userid))->save($userarr);
            }
            optMsgnum($table,$map,'add');

            //每天前20条留言有积分送
            $where['action']= array('EQ','message');
            $signnum=getCount('UserDynamic',$where);
           
             if($signnum <= 20){

               service('Passport')->userDynamic($this->userid,2,'评论留言获取2个积分',1,'message');
             }
            
            $this->success('感谢您的留言,正在刷新页面!');
        }else{
            $error = D('Home/Commons')->getError();
            $this->error($error ? $error : '留言失败,请稍候再试！');
        }
    }

    /**
     * 回复留言
     */
    public function replymsg(){
        $this->check();
        $post=I('post.');
        if(empty($post)){
            $this->error('回复出错,请稍候再试!');
        }
        if($post['touserid']==$this->userid){
            $this->error('您无权回复留言!');
        }
        $post['create_time']=time();
        $post['userid']=$this->userid;
		$post['parent_id'] = empty($post['parent_id']) ? 0 : intval($post['parent_id']);

        $type=$post['type'];
        if($type==1){
            $table='Works';
            $map['workid']=$post['infoid'];
        }elseif($type==2){
            $table='User';
            $map['userid']=$post['infoid'];
        }elseif($type==3){
            $table='TryReport';
            $map['id']=$post['infoid'];
        }elseif($type==4){
            $table='Suipai';
            $map['id']=$post['infoid'];
        }elseif($type==5){
            $table='News';
            $map['id']=$post['infoid'];
        }elseif($type==6){
            $table='Activity';
            $map['id']=$post['infoid'];
        }
        elseif($type==7)
        {
            $table='Teachlist';
            $map['teachid']=$post['infoid'];
        }

        if(D('Home/Commons')->sendMessage($post,$post['type'])){
            optMsgnum($table,$map,'add');

            //每天前20条留言有积分送
            $stime=strtotime(date('Y-m-d 00:00:00'));
            $etime=strtotime(date('Y-m-d 23:59:59'));
            $where['userid']= array('EQ',$this->userid);
            $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
            $where['action']= array('EQ','message');
            $signnum=getCount('UserDynamic',$where);
            if($signnum <= 20){

               service('Passport')->userDynamic($this->userid,2,'回复留言获取2个积分',1,'message');
            }
            $this->success('感谢您的回复,正在刷新页面!');
        }else{
            $error = D('Home/Commons')->getError();
            $this->error($error ? $error : '回复失败,请稍候再试！');
        }
    }

    /**
     * 删除留言
     */
    public function delmsg(){
        $model=M('UserMessage');
        $this->check();
        $get=I('get.');
        if(empty($get)){
            $this->error('删除出错,请稍候再试!');
        }
        $msginfo=$model->field(array('userid','type','infoid','parent_id'))->where(array('id'=>$get['msgid']))->find();
        if($msginfo['userid']!=$this->userid){
            $this->error('不是您的留言,您无权删除该条留言!');
        }
        $result=$model->where(array('id'=>$get['msgid']))->delete();
        if($result){
            $type=$msginfo['type'];
            $infoid=$msginfo['infoid'];
            if($type==1){
                $table='Works';
                $map['workid']=$infoid;
            }elseif($type==2){
                $table='User';
                $map['userid']=$infoid;
            }elseif($type==3){
                $table='TryReport';
                $map['id']=$infoid;
            }elseif($type==4){
                $table='Suipai';
                $map['id']=$infoid;
            }elseif($type==5){
                $table='News';
                $map['id']=$infoid;
            } elseif($type==6){
                $table='Activity';
                $map['id']=$infoid;
            }elseif($type == 7)
            {
                $table='Teachlist';
                $map['teachid'] = $infoid;
            }
  
            if($msginfo['parent_id'] == 0)
            {
            	$yf_temp = $model->field('id')->where(array('parent_id'=>$get['msgid']))->select();
            	if($yf_temp)
            	{
            		foreach($yf_temp as $k => $v)
            		{
            			$yf_result = $model->where(array('id'=>$v['id']))->delete();
            			if($yf_result)
            			{
            				optMsgnum($table,$map,'del');
            			}
            		}
            	}
            }
            optMsgnum($table,$map,'del');
            service('Passport')->userDynamic($this->userid,2,'删除留言减2个积分',0,'message');
            $this->success('删除留言成功!');
        }else{
            $this->error('删除留言失败');
        }

    }


    /**
     * 点赞
     */
    public function adddig(){
        $this->check();
        $get=I('get.');
        $get['userid']=$this->userid;
        $workuid=M('Works')->where(array('workid'=>$get['infoid']))->getField('userid');
        if($workuid==$this->userid){
            $this->error('您不能评价自己的作品!');
        }
        if(D('Home/Commons')->adddig($get,1)){
            if($get['digtype']==1){
                $this->success('成功点评','do/canceldig/');
            }elseif($get['digtype']==0){
                $this->success('成功点评','do/canceldig/');
            }
        }else{
            $error = D('Home/Commons')->getError();
            $this->error($error ? $error : '点赞失败,请稍候再试！');
        }
    }

    /**
     * 取消点赞
     */
    public function canceldig(){
        $this->check();
        $digModel=M('Dig');
        $get=I('get.');
        $where['userid']=$this->userid;
        $where['infoid']=$get['infoid'];
        $diginfo=$digModel->field('digtype,create_time')->where(array('userid'=>$this->userid,'infoid'=>$get['infoid']))->find();
        $btwtime=time()-$diginfo['create_time'];
        if($btwtime<30){
            $this->error(30-$btwtime.'秒后再试');
            die;
        }
        if($diginfo['digtype']==1){
            M('Works')->where(array('workid'=>$get['infoid']))->setDec('goodnum',1);
            $digModel->where($where)->save(array('status'=>false));
            $this->success('成功取消点评','do/adddig/');
        }elseif($diginfo['digtype']==0){
            M('Works')->where(array('workid'=>$get['infoid']))->setDec('badnum',1);
            $digModel->where($where)->save(array('status'=>false));
            $this->success('成功取消点评','do/adddig/');
        }

        die;
        $result=$digModel->where($where)->save($data);
        if($result){
            if($digtype==1){
                //echo 11111;die;
                M('Works')->where(array('workid'=>$get['infoid']))->setDec('goodnum',1);
                $this->success('成功取消点评','do/adddig/');
            }elseif($digtype==0){
                M('Works')->where(array('workid'=>$get['infoid']))->setDec('badnum',1);
                $this->success('成功取消点评','do/adddig/');
            }
        }else{
            $this->error('请勿操作过快');
        }
    }


    /**
     * 收藏作品操作
     */
    public function addfavorite(){
        $get=I('get.');
        $get['userid']=$this->userid;
        $this->check();
        $workuid=M('Works')->where(array('workid'=>$get['infoid']))->getField('userid');
        if($workuid==$this->userid){
            $this->error('您不能收藏自己的作品!');
        }
        if(D('Home/Commons')->addfavorite($get,1)){
            $this->success('取消收藏','do/cancelfavorite/');
        }else{
            $error = D('Home/Commons')->getError();
            $this->error($error ? $error : '收藏失败,请稍候再试！');
        }
    }

    /**
     * 取消收藏操作
     */
    public function cancelfavorite(){
        $this->check();
        $digModel=M('UserFavorite');
        $get=I('get.');
        $where['userid']=$this->userid;
        $where['infoid']=$get['infoid'];
        $data['status']=false;
        $result=$digModel->where($where)->save($data);
        if($result){
            M('Works')->where(array('workid'=>$get['infoid']))->setDec('favoritenum',1);
            $this->success('我要收藏','do/addfavorite/');
        }else{
            $this->error('取消操作失败');
        }
    }

    /**
     * 签到
     */
    public function sign(){
        $this->check();
        $stime=strtotime(date('Y-m-d 00:00:00'));
        $etime=strtotime(date('Y-m-d 23:59:59'));
        $where['userid']= array('EQ',$this->userid);
        $where['action']= array('EQ','sign');
        $where['create_time']=array(array('GT',$stime),array('LT',$etime),'and');
        $mysign=getCount('UserDynamic',$where);
        if($mysign>0){
            //表示今天已经签过到了
            $this->error('签到成功');

        }else{
            $stimes=strtotime(date('Y-m-d 00:00:00'))-6*24*3600;
            $wheres['userid']= array('EQ',$this->userid);
            $wheres['action']= array('EQ','sign');
            $wheres['credits'] = array('NEQ',10);
            $wheres['create_time']=array(array('GT',$stimes),'and');
            $signnum=getCount('UserDynamic',$wheres);

            if($signnum == 6){

                $result = service('Passport')->userDynamic($this->userid,10,'连续7天签到获取10个积分',1,'sign');
            
            }else{

                $result=service('Passport')->userDynamic($this->userid,2,'签到获取2个积分',1,'sign');

            }

            if($result){
                $this->success('签到成功');
            }else{
                $error = $result->getError();
                $this->error($error ? $error : '签到失败,请稍候再试！');
            }
        }
        
    }

}